﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SiteInfo_Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            //txtId.Attributes.Add("readonly", "readonly");
            //txtName.Attributes.Add("readonly", "readonly");
            bd();
        }
    }
    void bd()
    {
        int id =int.Parse(Request["sid"]);
        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        CMSModel.SiteInfo type = dal.SiteInfo.First(x => x.sid == id);
        
        txtId.Text = type.sid.ToString();
        txtName.Text = type.SiteName;
        txtUrl.Text = type.SiteUrl;
        
        ddlPid.DataSource = dal.SiteType.Where(x => (x.pid.Equals((object)null) && dal.SiteType.Where(y => y.pid == x.stid).Count() == 0)||((object)x.pid!=null));

        
       // ddlPid.DataSource = dal.SiteType;
        ddlPid.DataTextField = "name";
        ddlPid.DataValueField = "stid";
        ddlPid.DataBind();

        CMSModel.CMSEntities dal1 = DAL.DBManager.GetCmsEmtity();
        //var c = dal1.SiteType_SiteInfo.AsQueryable();
        var id1 =Server.UrlDecode(Request["sid"]);
        var tt = from a in dal1.SiteType_SiteInfo
                 from b in dal1.SiteType
                 from d in dal1.SiteInfo
                 where a.sid == d.sid && a.stid == b.stid
                 select new { a.id, a.stid, a.sid,d.SiteUrl, a.EditTime, b.name, d.SiteName };
        if (!string.IsNullOrEmpty(id1))
        {
            int sid = int.Parse(id1);
            tt = tt.Where(x => x.sid.Equals(sid));
        }


        Repeater1.DataSource = tt;
        Repeater1.DataBind();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {

        CMSModel.SiteType_SiteInfo type = new CMSModel.SiteType_SiteInfo();
        type.EditTime = DateTime.Now;
        type.sid =int.Parse(txtId.Text);
        type.stid = int.Parse(ddlPid.SelectedValue);

        CMSModel.CMSEntities dal = DAL.DBManager.GetCmsEmtity();
        try
        {
            var c = dal.SiteType_SiteInfo.Where(x => x.sid == type.sid && x.stid == type.stid).AsQueryable();
            if(c.Count()>0)
            {
                return;
            }
            dal.AddToSiteType_SiteInfo(type);
            dal.SaveChanges();
            bd();
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加成功'),document.location='editType.aspx?sid=" + txtId.Text+"';", true);
        }
        catch (Exception ex)
        {
            
            LogYuMin.LogWriter.GetWriter(LogYuMin.LogFile.error).Write(GetType(), "addSiteInfo", LogYuMin.LogStates.ERROR, ex);
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('添加失败:')", true);
        }

    }
}